package com.jww.controller.system;

import com.github.pagehelper.PageInfo;
import com.jww.controller.BaseServlet;
import com.jww.domain.system.User;
import com.jww.service.UserService;
import com.jww.service.impl.UserServiceImpl;
import com.jww.utils.BeanUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

// uri:/system/user?operation=list

@WebServlet("/system/user/*")
public class UserServlet extends BaseServlet {

    private UserService service=new UserServiceImpl();

    public void selectAll(HttpServletRequest req, HttpServletResponse resp){

        try {
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            List<User> list = service.selectAll(pageNum, pageSize);
            PageInfo<User> info=new PageInfo<>(list);
            req.setAttribute("page",info);
            req.getRequestDispatcher("/pages/system/user/list.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void list(HttpServletRequest req, HttpServletResponse resp){

        try {
            List<User> list = service.list();
            req.setAttribute("userList",list);

            req.getRequestDispatcher("/pages/system/user/add.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void save(HttpServletRequest req, HttpServletResponse resp){

        User user = BeanUtil.fillBean(req, User.class,"yyyy-MM-dd");

        System.out.println(user+".............................");
        service.save(user);
        selectAll(req,resp);

    }

    public void delete(HttpServletRequest req, HttpServletResponse resp){
        String ids = req.getParameter("ids");

        boolean flag = service.delete(ids);

        if (flag){
            selectAll(req,resp);
        }else {
            try {
                req.getRequestDispatcher("pages/error.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void selectById(HttpServletRequest req, HttpServletResponse resp){

        try {
            String id = req.getParameter("id");
            User user = service.selectById(id);
            List<User> userList = service.list();

            req.setAttribute("userList",userList);
            req.setAttribute("user",user);
            req.getRequestDispatcher("/pages/system/user/update.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void update(HttpServletRequest req, HttpServletResponse resp){
        User user = BeanUtil.fillBean(req, User.class,"yyyy-MM-dd");

        service.update(user);

        selectAll(req,resp);

    }
}
